package com.lxyk.controller.user;

import com.lxyk.domain.PageBean;
import com.lxyk.domain.SysRole;
import com.lxyk.domain.SysUser;
import com.lxyk.domain.UserInfo;
import com.lxyk.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有用户
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/findAll")
    public String findAll(Model model, Integer page, Integer size) {
        if (page == null || page < 0) {
            page = 0;
        }
        if (size == null || size < 0) {
            size = 10;
        }
        PageBean pageBean = userService.findAll(page, size);
        model.addAttribute("pageInfo", pageBean);
        return "user-list";
    }

    //添加用户
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/save")
    public String save(SysUser user, UserInfo userInfo, HttpServletRequest request) {
        boolean bool = userService.save(user, userInfo);
        //用户名重复
        if (!bool) {
            request.getSession().setAttribute("msg", "用户名重复!");
        }
        return "redirect:findAll";
    }

    //展示用户的角色列表(用于修改之前的展示, 用户拥有的角色会被勾上)
    //success用于判断修改用户角色是否成功,见addRoleToUser方法
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/toAddRolePage")
    public String toAddRolePage(Model model, Integer id, boolean success) {
        Map<String, Object> map = userService.toAddRolePage(id);
        model.addAttribute("uid", id);
        model.addAttribute("allRoles", map.get("allRoles"));
        model.addAttribute("myRoles", map.get("myRoles"));
        if (success) {
            model.addAttribute("success", "修改成功");
        }
        return "user-role-add";
    }

    //修改用户的角色列表
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(Integer[] ids, Integer userId) {
        userService.addRoleToUser(userId, ids);
        return "redirect:toAddRolePage?success=" + true + "&id=" + userId;
    }

    //删除用户
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/delete")
    public String delete(Integer id) {
        if (id != null) {
            userService.delete(id);
        }
        return "redirect:findAll";
    }

    //根据用户名查询用户信息,用户详情,角色信息
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/findByName")
    public String findByName(Model model, String username) {
        SysUser user = userService.findByName(username);
        model.addAttribute("user", user);
        //拼接角色信息
        String roles = "";
        for (SysRole role : user.getRoles()) {
            roles += " " + role.getRoleDesc();
        }
        model.addAttribute("roles", roles);
        return "user-show";
    }

    //更新用户详情
    @Secured({"ROLE_ADMIN"})
    @RequestMapping("/update")
    public String update(HttpServletRequest request, SysUser user) {
        boolean bool = userService.update(user);
        if (bool) {
            request.getSession().setAttribute("msg", "修改成功!");
        } else {
            request.getSession().setAttribute("msg", "修改失败!");
        }
        return "redirect:findAll";
    }

    //修改密码
    @RequestMapping("/editPassword")
    public String editPassword(String newPassword) {
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //设置新密码
        user.setPassword(newPassword);
        //修改密码
        userService.editPassword(user);
        return "redirect:/index.jsp";
    }

}
